<?php
namespace logic\admin\index;
use library\common\collections\ArrayUtils;
use model\AdminMenu;
use model\AdminPower;
use model\AdminRole;
use think\Db;
/**
 * User: zj
 * Date: 2017/4/26
 * Time: 15:11
 * Progress: 95%
 */
class LAdminRole
{

    //角色列表
    public function index_data($param){
        $model = new AdminRole();
        $where=array();
        if (!empty($keywords)) {
            $where['name'] = array('like', "%{$keywords}%"); //查询条件
        }
        $total=$model->where($where)->count(); //记录总数
        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['ordinal'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['id'] = 'asc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束
        $rows = $model
            ->where($where)
            ->limit($start, $page_size)
            ->order($sort)
            ->select();
        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );

        return $array;

    }

    //角色添加
    public function insert_data($param){

        $model = new AdminRole();
        $res=$model->validate(true)->save($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>'添加成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }

    //编辑页面
    public function edit_data($id){
        $model=new AdminRole();
        $data=$model->where(['id'=>$id])->find();
        return $data;
    }

    //管理员更新
    public function  update_data($id,$param){
        $model = new AdminRole();
        $res = $model->validate(true)->save($param,['id'=>$id]);
        if($res){
            return array(
                'success'=>true,
                'message'=>'修改成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }


    //管理员删除
    public function delete_data($id){
        $model = new AdminRole();
        $res = $model->where(['id'=>$id])->delete();
        if($res){
            return array(
                'success'=>true,
                'message'=>'删除成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()

            );
        }
    }


    //权限分配
    public function power_index($id){
        $model = new AdminPower();
        $where['flag_use'] = 1;
        $where['flag_delete'] = 0;
        $result = $model->where($where)->field("id,parent_id,name")->order('sort asc')->select();
        $list = json_decode(json_encode($result),true);
        $data = ArrayUtils::arrayToTree($list)['children'];


        $r = new AdminRole();
        $check=$r->where(['id'=>$id])->find();
        $powers=$check->getData()['powers_show'];
        $powers_show_arr=explode(',',$powers);
        $return=array(
            'data'=>$data['0']['children'],
            'powers_show_arr'=>$powers_show_arr
        );
        return $return;
    }

    function get_top_parentid($id,$ids=array()){
        $model = new AdminMenu();
        $r = $model->where(['id'=>$id])->find();
        if($r){
            $ids[]=$r->getData()['id'];
            if($r->getData()['parent_id'] != '0'){
                return $this->get_top_parentid($r['parent_id'],$ids);
            }
        }
        return $ids;
    }


    public function power_update($id,$param){
        $powers=$param['powers'];
        $powers_show=$param['powers_show'];

        $model = new AdminMenu();
        $menu_arr=array();
        $powers_insert='';
        for($i=0;$i<count($powers);$i++){
            if($powers_insert != null){
                $powers_insert=$powers_insert.','.$powers[$i];
            }else{
                $powers_insert=$powers[$i];
            }
            $menu_item=$model->where(['power_id'=>$powers[$i]])->find();
            if($menu_item){
                $menu_id=$menu_item->getData()['id'];
//                $powers_insert[]=$menu_id;
                $menu_id_arr=$this->get_top_parentid($menu_id);
                $menu_arr= array_merge($menu_arr,$menu_id_arr);
            }
        }
        $menu_arr=array_unique($menu_arr);
        $menu_arr = array_values($menu_arr);
        $menu='';
//        var_dump($menu_arr);
//        var_dump(count($menu_arr));die();
        for($i=0;$i<count($menu_arr);$i++){
            if($menu != null){
                $menu=$menu.','.$menu_arr[$i];
            }else{
                $menu=$menu_arr[$i];
            }
        }

        $new_data=array(
            'menus'=>$menu,
            'powers'=>$powers_insert,
            'powers_show'=>$powers_show
        );
//        var_dump($id);
//        var_dump($new_data);die();
        $model_role = new AdminRole();
        $res=$model_role->save($new_data,['id'=>$id]);
//        var_dump($res);die();
        if($res){
            return array(
                'success'=>true,
                'message'=>'修改成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }
    //权限更新
    public function power_update1($id,$param){
        $model = new AdminMenu();
        $data['powers'] = implode(",",$param);
        $menu = array();
        foreach($param as $v) {
            $m = $model->where(['power_id' => $v])->field("id,parent_id")->find();
            $data_menus = $this->get_menu_id($m['id'],$m['parent_id']);
            foreach ($data_menus as $t){
                if(!in_array($t,$menu)){
                    array_push($menu, $t);
                }
            }
        }
        $data['menus'] =implode(",",$menu);

        $model_role = new AdminRole();
        $res = $model_role->where(['id'=>$id])->save($data);
        if($res){
            return array(
                'success'=>true,
                'message'=>'修改成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }

    public function get_menu_id($id,$parent_id){
        $model = new AdminMenu();
        $menus = array();
        if($parent_id !== 0){
            $m = $model->where(['id'=>$parent_id])->field("parent_id")->find();
            $menus_son = $this->get_menu_id($parent_id,$m['parent_id']);
            if($menus_son){
                foreach($menus_son as $values){
                    if(!in_array($values,$menus)){
                        array_push($menus, $values);
                    }
                }
            }
        }else{
            if(!in_array($id,$menus)){
                array_push($menus, $id);
            }
        }

        return $menus;
    }


}